//
//  DetailViewController.m
//  预购清单
//
//  Created by  on 2022/10/17.
//

#import "DetailViewController.h"
#import "NodataFooter.h"
#import "DetailCell.h"
#import "DetailHeader.h"
#import "RankCell.h"
#import "MatchCell.h"

@interface DetailViewController ()
@property (nonatomic,strong) NSArray *homeArray;
@property (nonatomic,strong) NSArray *fromArray;
@property (nonatomic,strong) NSArray *futureArray;
@property (nonatomic,strong) NSArray *detailData;
@property (nonatomic, assign) DetailType type;
@property (nonatomic, assign) BOOL isfive;
@property (nonatomic,assign) CGFloat headerH;
@property (nonatomic,strong) NSArray *content;
@end

@implementation DetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)initUI{
    self.type = DetailTypeHistory;
    self.isfive = YES;
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.scrollEnabled = YES;
    [self.tableView registerNib:[DetailCell nib] forCellReuseIdentifier:[DetailCell ID]];
    [self.tableView registerNib:[RankCell nib] forCellReuseIdentifier:[RankCell ID]];
    [self.tableView registerNib:[MatchCell nib] forCellReuseIdentifier:[MatchCell ID]];
    self.tableView.c_top.equalTo(self.view.c_top).coffset(0);
    self.tableView.c_left.equalTo(self.view.c_left).coffset(0);
    self.tableView.c_right.equalTo(self.view.c_right).coffset(0);
    self.tableView.c_bottom.c_equal(self.view.c_bottom).coffset(0);
}
- (void)action:(NSInteger)tag{
    if (tag <= 203) {
        self.type = tag;
    }else if (tag == 204 || tag == 205){
        self.isfive = tag == 204;
    }else if(tag == 206){///全部赛事
        
    }else if (tag == 207){///返回
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else if (tag == 208){///收藏
            self.model.favorite = !self.model.favorite;
            [CCHud msg:self.model.favorite ? LS(@"已经收藏") : LS(@"取消收藏")];
            [self.model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [NotifyTool post:notifyRecordUpdate :nil];
                });
            }];
    }
}
- (void)loadData{
    if (self.model == nil) {
        return;
    }
    if (self.type != DetailTypeHistory) {
        [self.tableView reloadData];
    }
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [DataTool findName:self.model.home complete:^(NSMutableArray<Record *> * _Nonnull result) {
            self.homeArray = result;
            dispatch_group_leave(group);
        }];
    });
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [DataTool findName:self.model.from complete:^(NSMutableArray<Record *> * _Nonnull result) {
            self.fromArray = result;
            dispatch_group_leave(group);
        }];
    });
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [DataTool find:NO finish:NO complete:^(NSMutableArray<Record *> * _Nonnull result) {
            self.futureArray = result;
            dispatch_group_leave(group);
        }];
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSInteger start = self.isfive ? 5 : 10;
        NSInteger count1 = self.homeArray.count >= start ? start : self.homeArray.count;
        NSArray *array1 = [self.homeArray subarrayWithRange:NSMakeRange(0, count1)];
        NSInteger count2 = self.fromArray.count >= start ? start : self.fromArray.count;
        NSArray *array2 = [self.fromArray subarrayWithRange:NSMakeRange(0, count2)];
        NSInteger count3 = self.futureArray.count >= 3 ? 3 : self.futureArray.count;
        NSArray *array3 = [self.fromArray subarrayWithRange:NSMakeRange(0, count3)];
        self.detailData = @[@[@{self.model.home:array1},@{self.model.from:array2}],@[@{@"未来几场":array3}]];
        [self.tableView reloadData];
    });
}
- (void)setType:(DetailType)type{
    _type = type;
    [self loadData];
}
- (void)setIsfive:(BOOL)isfive{
    _isfive = isfive;
    [self loadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.type == DetailTypeHistory) {
        return 2;
    }else{
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.type == DetailTypeHistory) {
        return section == 0 ? 2 : 1;
    }else if (self.type == DetailTypeInfo){
        return 1;
    }else{
        return 2;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == DetailTypeHistory) {
        DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:[DetailCell ID]];
        cell.dict = self.detailData[indexPath.section][indexPath.row];
        return  cell;
    }else if (self.type == DetailTypeInfo){
        MatchCell *cell = [tableView dequeueReusableCellWithIdentifier:[MatchCell ID]];
        cell.model = self.model;
        return  cell;
    }else{
        RankCell *cell = [tableView dequeueReusableCellWithIdentifier:[RankCell ID]];
        cell.homename = indexPath.row == 0 ? LS(self.model.home) : LS(self.model.from);
        return  cell;
    }
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.type == DetailTypeHistory) {
        return 0;
    }else if (self.type == DetailTypeInfo){
        return 240;
    }else{
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.type == DetailTypeHistory) {
        self.headerH = section == 0 ? 300.0 / 375 * SCREEN_WIDTH + 119 : 0;
    }else if (self.type == DetailTypeInfo){
        self.headerH = section == 0 ? 300.0 / 375 * SCREEN_WIDTH + 119 - 64 : 0;
    }else if (self.type == DetailTypeRank){
        self.headerH = section == 0 ? 300.0 / 375 * SCREEN_WIDTH + 119 : 0;
    }
    return self.headerH;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (self.type != DetailTypeInfo) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , 240)];
    NodataFooter *footer = (NodataFooter*)[NodataFooter loadFromXIB];
    footer.frame = view.bounds;
    //    footer.img.image = [UIImage imageWithData:self.model.img];
    [view addSubview:footer];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section > 0) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , self.headerH)];
    DetailHeader *header = (DetailHeader*)[DetailHeader loadFromXIB];
    header.frame = view.bounds;
    header.type = self.type;
    header.isfive = self.isfive;
    header.favoriteBtn.selected = self.model.favorite;
    header.bgimg.image = [UIImage imageNamed:self.model.isbasketball?@"篮球详细背景":@"足球详细背景"];
    header.name.text = LS(self.model.league_name);
    WS(self)
    header.block = ^(NSInteger tag) {
        [weakself action:tag];
    };
    [view addSubview:header];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

@end

